using System.Text.RegularExpressions;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class RegexProductionSpecification {
        [Test]
        public void ShouldParseLengthOfRegex () {
            var regex = new RegexProduction (@"\d=\d");

            ParseResult result = regex.Parse ("5=6h", 0, new ParseContext ());

            Assert.IsNotNull (result);
            Assert.AreEqual (3, result.Index);
            var term = result.Term as StringTerm;
            Assert.IsNotNull (term);
            Assert.AreEqual ("5=6", term.Value);
            Assert.AreEqual (new SourceInformation (null, 0, 3), term.SourceInformation);
        }

        [Test]
        public void ShouldNotParseIfRegexDoesNotMatch() {
            AssertNotParsed(@"\d=\d", "5=a");
            AssertNotParsed(@"\d=\d", "53=3");
            AssertNotParsed(@"\d=\d", " 3=2");
        }

        private static void AssertNotParsed (string pattern, string source) {
            var regex = new RegexProduction (pattern);

            ParseResult result = regex.Parse (source, 0, new ParseContext ());

            Assert.IsNull (result);
        }
    }
}